package com.ruicar.afs.cloud.manage.workflow.processor;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.admin.api.dto.UserSimpleInfoDto;
import com.ruicar.afs.cloud.admin.api.feign.AfsRoleFeign;
import com.ruicar.afs.cloud.admin.api.feign.AfsUserFeign;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.security.vo.RoleVo;
import com.ruicar.afs.cloud.common.core.security.vo.UserVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.core.util.SpringContextHolder;
import com.ruicar.afs.cloud.manage.common.entity.ManageUpcomeTasksRecord;
import com.ruicar.afs.cloud.manage.common.service.ManageUpcomeTasksRecordService;
import com.ruicar.afs.cloud.manage.workflow.enums.QueryRoleType;
import com.ruicar.afs.cloud.manage.workflow.enums.WorkflowTypeEnum;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.CommonGetRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.GetAssignUserRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.UserOnlineRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.response.*;
import com.ruicar.afs.cloud.workflow.gwt.processor.AfsWorkFLowQueryProcessor;
import com.ruicar.afs.cloud.workflow.gwt.processor.DefaultAfsWorkFLowQueryProcessor;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 工作流程回调
 * @Author xudongdong
 * @Date 2020/07/17
 */
@Component
@Slf4j
@AllArgsConstructor
public class ContractWorkFLowQueryProcessor implements AfsWorkFLowQueryProcessor {

    private final ManageUpcomeTasksRecordService manageUpcomeTasksRecordService;
    private final AfsUserFeign afsUserFeign;
    private final AfsRoleFeign afsRoleFeign;

    @Override
    public List<String> afsFLowKeys() {
        List list = new ArrayList();
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.INSURANCE_CLAIM));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.INSURANCE_REMNEW));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.INSURANCE_CHANGE_EXIT));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.CONTRACT_CANCEL));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.CONTACT_CHANGE));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.COST_EXEMTP_BILL));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.CUSTINFO_CHANGE));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.BANK_CHANGE));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.EARLY_SETTLEMENT));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.CONTRACT_REISSUR));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.PENALTY_EXEMPT));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.COST_EXEMPT));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.COLLECTION_REVOKE));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.CONTRACT_OVERPAYMENT));
        list.add(AfsEnumUtil.key(WorkflowTypeEnum.REVOKE_HANDLE));
        return list;
    }

    @Override
    public List<UserOnlineResponse> checkUserOnline(UserOnlineRequest userOnlineRequest) {
        log.info("在岗用户获取 ==> {}", userOnlineRequest);
        List<UserOnlineResponse> responses = new ArrayList<>();
        List<String> userNameList = userOnlineRequest.getUserInfos().stream().map(UserOnlineRequest.UserInfo::getUserName).collect(Collectors.toList());
        log.info("在岗用户响应 ==> {}", responses);
        return responses;
    }

    @Override
    public GetAssignUserResponse getAssignUser(GetAssignUserRequest getAssignUserRequest) {
        try {
            IResponse<List<UserSimpleInfoDto>> listIResponse = afsUserFeign.getSimpleUserListByRoleCode(getAssignUserRequest.getRoleCode());
            Assert.isTrue(CommonConstants.SUCCESS.equalsIgnoreCase(listIResponse.getCode()), "feign请求用户异常");
            return listIResponse
                    .getData()
                    .stream()
                    .map(userSimpleInfoDto -> {
                        GetAssignUserResponse getAssignUserResponse = new GetAssignUserResponse();
                        getAssignUserResponse.setUserName(userSimpleInfoDto.getUserName());
                        return getAssignUserResponse;
                    }).findAny().get();
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

    @Override
    public QueryOrgConditionResponse queryOrgCondition(CommonGetRequest commonGetRequest) {
        return SpringContextHolder.getBean(DefaultAfsWorkFLowQueryProcessor.class).queryOrgCondition(commonGetRequest);
    }

    @Override
    public QueryRoleResponse queryRole(CommonGetRequest commonGetRequest) {
        log.info("设计器处理人类型获取 ==> {}", commonGetRequest);
        try {
            IResponse<List<RoleVo>> iResponse = afsRoleFeign.getAllList();
            Assert.isTrue(CommonConstants.SUCCESS.equalsIgnoreCase(iResponse.getCode()), "feign请求用户异常");
            QueryRoleResponse queryRoleResponse = new QueryRoleResponse();
            iResponse.getData().add(getRoleVO(QueryRoleType.APPOINT_USER));
            queryRoleResponse.setRoleVos(iResponse.getData());

            return queryRoleResponse;
        } catch (Exception e) {
            log.error("", e);
        }
        return null;
    }

    private RoleVo getRoleVO(QueryRoleType roleType) {
        RoleVo roleVo = new RoleVo();
        roleVo.setRoleCode(AfsEnumUtil.key(roleType));
        roleVo.setRoleName(AfsEnumUtil.desc(roleType));
        return roleVo;
    }

    @Override
    public Page<QueryUserResponse> queryUserInfo(QueryCondition<Map<String, Object>> queryCondition) {
        return SpringContextHolder.getBean(DefaultAfsWorkFLowQueryProcessor.class).queryUserInfo(queryCondition);
    }

    @Override
    public UserVo getRtnUserInfo(CommonGetRequest commonGetRequest) {
        return SpringContextHolder.getBean(DefaultAfsWorkFLowQueryProcessor.class).getRtnUserInfo(commonGetRequest);
    }

    @Override
    public UserVo getApplyUserInfo(CommonGetRequest commonGetRequest) {
        log.info("经销商信息获取 ==> {}", commonGetRequest);
        return SpringContextHolder.getBean(DefaultAfsWorkFLowQueryProcessor.class).getApplyUserInfo(commonGetRequest);
    }

    @Override
    public UserVo getLeader(CommonGetRequest commonGetRequest) {
        return null;
    }

    @Override
    public UserVo getOperUser(CommonGetRequest commonGetRequest) {
        log.info("指定处理人获取 ==> {}", commonGetRequest);
        ManageUpcomeTasksRecord manageUpcomeTasksRecord = manageUpcomeTasksRecordService.getOne(Wrappers.<ManageUpcomeTasksRecord>query().lambda()
                        .eq(ManageUpcomeTasksRecord::getBizDataId, commonGetRequest.getBizDataId())
                        .orderByDesc(ManageUpcomeTasksRecord::getCreateTime)
                , false
        );
        UserVo response = new UserVo();
        response.setUsername(manageUpcomeTasksRecord.getCreateBy());
        log.info("指定处理人响应 ==> {}", response.getUsername());
        return response;
    }

}
